package com.vanzo;

import android.app.Application;
import android.content.Context;
import android.content.SharedPreferences;

import com.vanzo.bean.Follower;
import com.vanzo.bean.Master;
import com.vanzo.encryption.Encryption;
import com.vanzo.greendao.FollowerDaoUtil;
import com.vanzo.greendao.MasterDaoUtil;
import com.vanzo.instruction.Instruction;
import com.vanzo.service.Talkie;
import com.vanzo.util.LogUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by tisen on 2018/3/1.
 */

public class Tpplication extends Application {


    private static final String SHARED_PREFERENCE = "talkie_share";
    private static final String FOLLOWERS_COUNT = "followers_count";
    private static final String CHANNEL_MODE = "channel_mode";
    private static final String TFV_DEFAULT = "tfv_default";
    private static final String RFV_DEFAULT = "rfv_default";
    private static final String VOLUME_DEFAULT = "volume_default";
    private static final String MIC_DEFAULT = "mic_default";
    private static final String SELECT_CHANNEL_ID = "select_default_id";
    private static final String IS_LOCKED = "is_locked";


    private Instruction instruction;
    private Talkie talkie;
    private SharedPreferences preferences;
    private MasterDaoUtil masterDaoUtil;
    private FollowerDaoUtil followerDaoUtil;
    private Master master;

    @Override
    public void onCreate() {
        super.onCreate();
        LogUtil.put(this, "onCreate", LogUtil.WARN);
        instruction = new Instruction(this);
        talkie = new Talkie(this);
        preferences = getSharedPreferences(SHARED_PREFERENCE, Context.MODE_PRIVATE);
        initDate();
    }

    private void initDate() {
        lockedChannelId = preferences.getInt(SELECT_CHANNEL_ID, 0);
        locked = preferences.getBoolean(IS_LOCKED, false);
        masterDaoUtil = new MasterDaoUtil(this);
        followerDaoUtil = new FollowerDaoUtil(this);
//        instruction.setPower(true);
        instruction.start();
    }

    public Instruction getInstruction() {
        return instruction;
    }

    public Talkie getTalkie() {
        return talkie;
    }

    public List<Channel> getChannels() {
        List<Channel> channels = new ArrayList<>();
        channels.add(new Channel(0, 0, "409.7500", "409.7500"));
        channels.add(new Channel(1, 0, "419.7500", "419.7500"));
        channels.add(new Channel(2, 0, "429.7500", "429.7500"));
        channels.add(new Channel(3, 1, "409.7500", "409.7500"));
        channels.add(new Channel(4, 1, "419.7500", "419.7500"));
        channels.add(new Channel(5, 1, "429.7500", "429.7500"));
        channels.add(new Channel(6, 0, "439.7500", "439.7500"));
        channels.add(new Channel(7, 0, "449.7500", "449.7500"));
        channels.add(new Channel(8, 1, "459.7500", "459.7500"));
        channels.add(new Channel(9, 1, "469.7500", "469.7500"));
        return channels;
    }


    private int lockedChannelId;

    public int getLockedChannelId() {
        return lockedChannelId;
    }

    public void setLockedChannelId(int lockedChannelId) {
        this.lockedChannelId = lockedChannelId;
        preferences.edit().putInt(SELECT_CHANNEL_ID, lockedChannelId).apply();
    }

    public class Channel {
        private int id;
        private int channel;
        private String tfreq;
        private String rfreq;

        public Channel(int id, int channel, String tfreq, String rfreq) {
            this.id = id;
            this.channel = channel;
            this.tfreq = tfreq;
            this.rfreq = rfreq;
        }

        public int getId() {
            return id;
        }

        public void setId(int id) {
            this.id = id;
        }

        public int getChannel() {
            return channel;
        }

        public void setChannel(int channel) {
            this.channel = channel;
        }

        public String getTfreq() {
            return tfreq;
        }

        public void setTfreq(String tfreq) {
            this.tfreq = tfreq;
        }

        public String getRfreq() {
            return rfreq;
        }

        public void setRfreq(String rfreq) {
            this.rfreq = rfreq;
        }
    }

    public List<Follower> getFollowers() {
        return followerDaoUtil.queryAllFollower();
    }


    public void initMasterFollowers(int groupId, int count, int first, int channel) {
        List<Follower> followers = new ArrayList<>();
        followerDaoUtil.deleteAll();
        Master master = getMaster();
        master.setGroupId(groupId);
        master.setFirstId(first);
        master.setFollowerSize(count);
        master.setChannelId(channel);
        updateMaster(master);
        int i = 1;
        while (i <= count) {
            Follower follower = new Follower(i, Encryption.getKillKey());
            followers.add(follower);
            i++;
        }
        followerDaoUtil.insertMultFollower(followers);
    }

    public void initFollowers(int groupId, int count, int first, int channel) {
        List<Follower> followers = new ArrayList<>();
        followerDaoUtil.deleteAll();
        Master master = getMaster();
        master.setGroupId(groupId);
        master.setFirstId(first);
        master.setFollowerSize(count);
        master.setChannelId(channel);
        updateMaster(master);
        int i = 1;
        while (i <= count) {
            Follower follower = new Follower(i);
            followers.add(follower);
        }
        followerDaoUtil.insertMultFollower(followers);
    }

    private boolean locked;

    public boolean isLocked() {
        return locked;
    }

    public void setLocked(boolean locked) {
        this.locked = locked;
        preferences.edit().putBoolean(IS_LOCKED, locked).apply();
    }

    public Master getMaster() {
        return masterDaoUtil.getMaster();
    }

    public void updateMaster(Master master) {
        if (master.getId() == null) {
            masterDaoUtil.insertMaster(master);
        } else {
            masterDaoUtil.updateMaster(master);
        }
    }

    public void bindMaster(Master master) {
        masterDaoUtil.updateMaster(master);
        initFollowers(master.getGroupId(), master.getFollowerSize(), master.getFirstId(), master.getChannelId());
    }

    public void updateFollower(Follower follower) {
        followerDaoUtil.updateFollower(follower);
    }

    @Override
    public void onTerminate() {
        super.onTerminate();
        LogUtil.put(this, "onTerminate", LogUtil.WARN);
    }
}
